package com.imooc.mall.controller;

import com.imooc.mall.common.ResultInfo;
import com.imooc.mall.form.OrderCreateForm;
import com.imooc.mall.pojo.User;
import com.imooc.mall.service.IOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static com.imooc.mall.common.MallConst.CURRENT_USER;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private IOrderService orderService;

    @PostMapping("/create")
    public ResultInfo create(@RequestBody @Valid OrderCreateForm orderCreateForm,
                             HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return orderService.create(user.getId(), orderCreateForm.getShippingId());
    }

    @GetMapping("/list")
    public ResultInfo list(@RequestParam(required = false, defaultValue = "pageNo") Integer pageNo,
                           @RequestParam(required = false, defaultValue = "pageNo") Integer pageSize,
                           HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return orderService.list(user.getId(), pageNo, pageSize);
    }

    @GetMapping("/detail/{orderNo}")
    public ResultInfo list(@PathVariable Long orderNo,
                           HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return orderService.detail(user.getId(),orderNo);
    }

    @PutMapping("/cancel/{orderNo}")
    public ResultInfo cancel(@PathVariable Long orderNo,
                           HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute(CURRENT_USER);
        return orderService.cancel(user.getId(),orderNo);
    }
}
